using System;
using System.Collections.Generic;

namespace Activa.ProMesh
{
    public class CSLoggingProvider : IPageLoggingProvider
    {
        public static int LogTraffic(int pageLogId, int sessionId, int visitorId, string pageId, string languageCode)
        {
            TrafficEntry traffic = TrafficEntry.New();

            traffic.SessionID = sessionId;
            traffic.PageLogID = pageLogId;
            traffic.VisitorID = visitorId;
            traffic.Page = pageId;
            traffic.Language = languageCode;

            traffic.Save();

            if (traffic.Session.RealSession == null)
            {
                traffic.Session.RealSession = true;
                traffic.Session.Save();
            }

            return traffic.TrafficID;
        }

        public int LogPage(string sessionId, string pageUrl, string queryString, string languageCode,string layoutName, string viewName, int milliSecondsRun, int milliSecondsRender, int milliSecondsTotal)
        {
            if (queryString.Length > 1 && queryString.Substring(0, 1) == "?")
                queryString = queryString.Substring(2);

            PageLogEntry log = PageLogEntry.New();

            log.PageID = pageUrl;
            log.QueryString = queryString;
            log.SessionID = int.Parse(sessionId ?? "0");
            log.LanguageID = 0;
            log.Elapsed = milliSecondsTotal;
            log.ElapsedRender = milliSecondsRender;
            log.ElapsedRun = milliSecondsRun;

            log.Save();

            return log.PageLogID;
        }

    }
}
